# include <iostream>
# include <fstream>
# include <cstdio>
# include <cstring>
# include <deque>
# include <unistd.h>
# include "files.hh"
# include <vector>
# include "demsStr.hh"

int rm_BlankLine(std::string filename)
{
    std::fstream targetFile(filename,std::fstream::in | std::fstream::out);
    std::string line;//作为读取的每一行
    std::string temp;//作为缓存使用
    std::deque<std::string> noBlankLineQueue;//双向队列,只在队首和队尾操作时性能较好
        //判断文件是否打开
    if(!targetFile){
        std::cerr << "Can't Open File!" << std::endl;
        return EXIT_FAILURE;        
    }
    //记录文件开始位置
    auto StartPos = targetFile.tellp();
//循环读取判断文件的空行并放入队列
    while(getline(targetFile,line))
	{
        // std::cout << targetFile.tellg() << std::endl;;    
        if(line.empty())
		{
            // std::cout << "此行为空" << std::endl; 
        }
		else
		{
            
			// std::cout << "上一行是空行" << std::endl;   
            noBlankLineQueue.push_back(line);
        }   
    }
    targetFile.close(); 
    //使用ofstream打开再关闭文件是为了清空源文件
    std::ofstream emptyFile(filename);
    emptyFile.close();
    //重新打开文本文件
    std::fstream target(filename,std::fstream::out | std::fstream::in);
    if(!target){
        std::cerr << "Can't Open File" << std::endl;
    }
    //获取队首和队尾
    auto begin = noBlankLineQueue.begin();
    auto end = noBlankLineQueue.end();
    //遍历双向队列中的元素
    //并写回文件
    while(begin != end){
        temp = *begin;
        // std::cout << temp << std::endl;
        target << temp << std::endl; 
        ++begin;
    }
    target.close();
    return EXIT_SUCCESS;
}

int CountLines(std::string filename)
{
    rm_BlankLine(filename);
    std::ifstream ReadFile;

    int n=0;
    char line[5120];
    ReadFile.open(filename.c_str(),std::ios::in);//ios::in 表示以只读的方式读取文件
    if(ReadFile.fail())//文件打开失败:返回0
    {
        return 0;
    }
    else//文件存在
    {
        while(!ReadFile.eof())
    {
        ReadFile.getline(line,5120,'\n');
        n++;
    }
        return n;
    }

    ReadFile.close();
}

std::string getLineOFile(std::string pathstr, std::string mode,int lineNum)
{
    std::FILE *file;
    char path[5120];
    strcpy( path, pathstr.c_str());
    int count;
    char trush[5120], resChar[5120];
    if (mode == "b" || mode == "begin") // 指begin 即从开头开始读
    {
        file = std::fopen(path, "r");
        for(count=lineNum-1; count!=0; count--)
	    {
		    fgets(trush, 5120, file);
		
	    }
        
        fgets(resChar, 5120, file);
        std::string resStr(resChar);
        return resStr;
    }
    else if(mode == "e" || mode == "end") // 值begin 即获取倒数第x行的
    {
        int lines = CountLines(path);
        int destLinenum = lines - lineNum +1;
        file = std::fopen(path, "r");
        for(count=destLinenum-1; count!=0; count--)
	    {
		    fgets(trush, 5120, file);
		
	    }
        
        fgets(resChar, 5120, file);
        std::string resStr(resChar);

        return resStr;
    }



}

std::string getFmFile_starting(std::string fpath, std::string info)
{
    char lineInfo[5120];
    std::string strLineInfo;
    std::ifstream file;
    file.open(fpath, std::ios::in);
    while(!file.eof())
    {
        file.getline(lineInfo, 5120);
        strLineInfo = lineInfo;
        std::vector<std::string> infovec = strSplit(strLineInfo, " ");
        if(infovec[0] == info)
        {
            return strLineInfo;
        }
    }
    return "FAIL TO FIND IN PATH\n" + fpath;
    
    


}

std::vector<std::string> vecFile(std::string fpath)
{
    std::ifstream ckFle(fpath, std::ios::in);
    std::vector<std::string> strvec;
    while(!ckFle.eof())
    {
        char temp[5120];
        std::string tmpstr;

        ckFle.getline(temp, 5120);
        tmpstr = temp;

        strvec.push_back(tmpstr);
    }

    ckFle.close();
    return strvec;
}
